﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace IIFProcessor
{
    internal class CategoryMappings
    {
        private string mappingsFile = "SavedMappings.xml";
        private string filePath;
        private Dictionary<string, Dictionary<string, string>> mappings;
       // private string[] matchList;
        private List<KeyValuePair<string,string>> matchListAll;
        private int matchListPointer;
        public CategoryMappings()
        {
            filePath = Utilities.GetCurrentPath() + "/" + mappingsFile;
            try
            {
                mappings = new Dictionary<string, Dictionary<string, string>>();

                if (File.Exists(filePath))
                {
                    ReadMappings();
                }
            }
            catch
            {
                throw new Exception("Error reading saved mappings from " + filePath);
            }
        }
        public void PersistMappings()
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml("<searches/>");
            foreach (string entry in mappings.Keys)
            {
                XmlElement searchElem = doc.CreateElement("search");
                XmlAttribute searchKey = doc.CreateAttribute("key");
                searchKey.Value = entry;
                searchElem.Attributes.Append(searchKey);
                doc.DocumentElement.AppendChild(searchElem);
                foreach (KeyValuePair<string, string> mapping in mappings[entry])
                {
                    XmlElement mappingElem = doc.CreateElement("mapping");
                    XmlAttribute mappinghKey = doc.CreateAttribute("key");
                    mappinghKey.Value = mapping.Key;
                    mappingElem.Attributes.Append(mappinghKey);
                    XmlAttribute mappinghValue = doc.CreateAttribute("value");
                    mappinghValue.Value = mapping.Value;
                    mappingElem.Attributes.Append(mappinghValue);
                    searchElem.AppendChild(mappingElem);
                }
            }
            doc.Save(filePath);
        }

        public string GetMapping(string searchText, string match)
        {
            if (mappings.ContainsKey(searchText))
            {
                Dictionary<string, string> mapps = mappings[searchText];
                if (mapps.ContainsKey(match))
                {
                    return mapps[match];
                }
                else
                {
                    return string.Empty;
                }

            }
            else
            {
                mappings.Add(searchText, new Dictionary<string, string>());
                return string.Empty;
            }
        }

        public void SetMapping(string searchText, string match, string mapping)
        {
            if (!mappings.ContainsKey(searchText))
            {
                mappings.Add(searchText, new Dictionary<string, string>());
            }
            Dictionary<string, string> mapps = mappings[searchText];
            mapps[match] = mapping;

        }
        private void ReadMappings()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(filePath);
            foreach (XmlElement search in doc.DocumentElement.SelectNodes("search"))
            {
                string searchKey = search.Attributes["key"].Value;
                mappings.Add(searchKey, new Dictionary<string, string>());
                Dictionary<string, string> mappingsDictionary = mappings[searchKey];
                foreach (XmlElement mapping in search.SelectNodes("mapping"))
                {
                    string mappingKey = mapping.Attributes["key"].Value;
                    string mappingValue = mapping.Attributes["value"].Value;
                    mappingsDictionary.Add(mappingKey, mappingValue);
                }
            }
        }
        #region MappingsCycling
        public void ResetCycling(string searchText)
        {
            matchListPointer = 0;
            if (mappings.ContainsKey(searchText))
            {
               // matchList = mappings[searchText].Keys.ToArray();   
                matchListAll = mappings[searchText].ToList<KeyValuePair<string,string>>();
            }
            else
            {
               // matchList = new string[0];
                matchListAll = new List<KeyValuePair<string, string>>(0);
            }

        }
        public KeyValuePair<string,string> NextMatch
        {
            get
            {
                if (matchListPointer < matchListAll.Count)
                {
                    return matchListAll[matchListPointer++];
                }
                else
                {
                    return new KeyValuePair<string,string>(string.Empty,string.Empty);
                }
            }
        }
        #endregion
    }
}
